<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    cancelText="关闭">
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
      <a-button v-if="!disableSubmit" type="primary" @click="handleOk">确定</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>

          <a-col :span="12">
            <a-form-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name']" :disabled="disableSubmit" placeholder="请输入合同名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['agreementCode']" :disabled="disableSubmit" placeholder="请输入合同编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同模板名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" :disabled="disableSubmit" @change="selectChange" v-decorator="['templateId',validatorRules.templateId]" :trigger-change="true" dictCode="ClmTemplate,name,id, statusId = 2" placeholder="请选择合同模板"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="签约时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择签约时间" :disabled="disableSubmit" v-decorator="['signDate',validatorRules.signDate]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag  type="list" :disabled="disableSubmit" @change="selectType" v-decorator="['typeId', validatorRules.typeId]" :trigger-change="true" dictCode="DataDictionary,description,dictionaryCode,parentId = root&dictionaryTypeId = AgreementType" placeholder="请选择输入合同类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="具体合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <a-select v-decorator="['specificTypeId']" :disabled="disableSubmit" style="width: 100%" placeholder="请选择具体合同类型">
                <a-select-option v-for="specificId in specificList" :key="specificId.dictionaryId">
                     {{ specificId.description }}
                </a-select-option>
              </a-select> -->
              <j-dict-select-tag :disabled="disableSubmit" type="list" v-decorator="['specificTypeId', validatorRules.typeId]" :trigger-change="true" dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = AgreementType, " placeholder="请选择输入具体合同类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="甲方" :labelCol="{ xs: { span: 24 }, sm: { span: 4 }}" :wrapperCol="{ xs: { span: 24 }, sm: { span: 20 }}">
              <j-search-select-tag v-if="fromCompanyTypeId == 'PERSON'" :disabled="disableSubmit"
                                   :async="false"
                                   dict="Person,name,partyId,typeId=EMPLOYEE"
                                   v-decorator="['fromCompanyId',validatorRules.fromCompanyId]"
                                   :trigger-change="true"
                                   placeholder="请选择甲方"
              />

              <j-search-select-tag  v-else-if="fromCompanyTypeId == 'GROUP'" :disabled="disableSubmit"
                                    :async="false"
                                    dict="ClmCompany,companyName,id,statusId=2"
                                    v-decorator="['fromCompanyId',validatorRules.fromCompanyId]"
                                    :trigger-change="true"
                                    placeholder="请选择甲方"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="乙方" :labelCol="{ xs: { span: 24 }, sm: { span: 4 }}" :wrapperCol="{ xs: { span: 24 }, sm: { span: 20 }}">
              <j-search-select-tag v-if="toCompanyTypeId == 'PERSON'" :disabled="disableSubmit"
                                   :async="false"
                                   dict="Person,name,partyId,typeId=EMPLOYEE"
                                   v-decorator="['toCompanyId',validatorRules.toCompanyId]"
                                   :trigger-change="true"
                                   placeholder="请选择乙方"
              />
              <j-search-select-tag  v-else-if="toCompanyTypeId == 'GROUP'" :disabled="disableSubmit"
                                    :async="false"
                                    dict="ClmCompany,companyName,id,statusId=2"
                                    v-decorator="['toCompanyId',validatorRules.toCompanyId]"
                                    :trigger-change="true"
                                    placeholder="请选择乙方"
              />
            </a-form-item>
          </a-col>


          <a-col :span="12">
            <a-form-item label="合同生效时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择合同生效时间" :disabled="disableSubmit" v-decorator="['fromDate']" :trigger-change="true"  style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同到期时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择合同到期时间" :disabled="disableSubmit" v-decorator="['thruDate']" :trigger-change="true"  style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['agreementAmount']" :disabled="disableSubmit" placeholder="请输入合同金额(元)" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" v-for="item in itemList" :key="item.id">
            <a-form-item :label="item.name" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit"
                       v-if="item.fieldTypeId == '1'"
                       v-model="item.description"
                       :placeholder="'请输入' + item.name"
              ></a-input>
              <j-date :disabled="disableSubmit"
                      v-if="item.fieldTypeId == '3'"
                      :placeholder="'请选择' + item.name"
                      v-model="item.description"
                      :trigger-change="true"
                      style="width: 100%"
              />
              <!--
                <j-dict-select-tag type="list" :disabled="disableSubmit" v-model="item.description" v-if="item.fieldTypeId == '2'"
                 :trigger-change="true" :dictCode="item.entityId+',description,dictionaryCode, dictionaryTypeId = '+item.dictionaryTypeId" :placeholder="'请选择'+item.name"/>
              -->
              <j-dict-select-tag type="list"
                                 :disabled="disableSubmit"
                                 v-decorator="['item.description']"
                                 v-if="item.fieldTypeId == '2'"
                                 :trigger-change="true"
                                 :dictCode="item.entityId+',description,dictionaryCode, dictionaryTypeId = '+item.dictionaryTypeId"
                                 :placeholder="'请选择'+item.name"/>
              <j-image-upload
                v-if="item.fieldTypeId == '5'"
                :disabled="disableSubmit"
                :isMultiple="false"
                v-model="item.description"
              ></j-image-upload>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction,getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '../../../../components/dict/JSearchSelectTag'
  import JImageUpload from '@/components/jeecg/JImageUpload'

  export default {
    name: "ClmAgreementModal",
    components: {
      JDate,
      JDictSelectTag,
      JSearchSelectTag,
      JImageUpload
    },
    data () {
      return {
        disableSubmit: false,
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        itemList: [],
        newItemList:[],
        ClmAgreementItem:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          templateId: {
            rules: [
              { required: true, message: '请选择合同模板名称!'},
            ]
          },
          signDate: {
            rules: [
              { required: true, message: '请选择签约时间!'},
            ]
          },
          typeId: {
            rules: [
              { required: true, message: '请选择合同类型!'},
            ]
          },
          fromCompanyId: {
            rules: [
              { required: true, message: '请选择甲方!'},
            ]
          },
          toCompanyId: {
            rules: [
              { required: true, message: '请选择乙方!'},
            ]
          },
        },
        url: {
          add: "/online/special/api/createClmAgreement",
          edit: "/online/special/api/updateClmAgreement",
          get: "/online/api/getClmAgreement",
          getClmTemplateAndItem: "/online/api/getClmTemplateAndItem",
          selectDataDictionary:  "/online/api/selectDataDictionary",
        },
        fromCompanyTypeId:'GROUP',
        toCompanyTypeId:'GROUP',
        specificList: []
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.visible = true;
        this.model = {};
        if(record.id){
          httpAction(this.url.get,{id:record.id},"post").then((res)=>{
            if(res.success){
              this.model = Object.assign({}, res.result);
              delete this.model['newItemList'];
              this.newItemList = res.result.itemList;
              if(this.newItemList){
                let itemList = [];
                this.newItemList.forEach(item=>{
                  itemList.push({fieldTypeId:item.fieldTypeId,description:item.description,name:item.name})
                })
                this.itemList = itemList;
              }
              if(this.model.fromCompanyTypeId)this.fromCompanyTypeId = this.model.fromCompanyTypeId;
              if(this.model.toCompanyTypeId)this.toCompanyTypeId = this.model.toCompanyTypeId;
              console.log(this.fromCompanyTypeId);
              this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model,'name','agreementCode','templateId','typeId','specificTypeId','fromCompanyId','toCompanyId','signDate','agreementAmount','fromDate','thruDate','createDate','createBy','statusId'))
              })
            }
          })
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
              this.model.statusId = '0';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            if(this.fromCompanyTypeId)this.model.fromCompanyTypeId = this.fromCompanyTypeId;
            if(this.toCompanyTypeId)this.model.toCompanyTypeId = this.toCompanyTypeId;
            let formData = Object.assign(this.model, values);
            if(this.itemList){
              this.ClmAgreementItem = [];
              this.itemList.forEach(item=>{
                this.ClmAgreementItem.push({name:item.name,description:item.description,fieldTypeId:item.fieldTypeId})
              })
              formData.ClmAgreementItem = this.ClmAgreementItem;
            }
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success('提交成功!');
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','agreementCode','templateId','typeId','specificTypeId','fromCompanyId','toCompanyId','signDate','agreementAmount','fromDate','thruDate','createDate','createBy','statusId'))
      },
      selectChange(e){
        if(e == undefined){
          return false
        }else{
          getAction(this.url.getClmTemplateAndItem,{id: e}).then(res => {
            if(res.success){
              this.itemList = res.result.clmTemplateItemList
            }
          })
        }
      },
      selectType(e){
        if(e == 'labor'){
          this.fromCompanyTypeId = 'GROUP';
          this.toCompanyTypeId = 'PERSON';
        }else{
          this.fromCompanyTypeId = 'GROUP';
          this.toCompanyTypeId = 'GROUP';
        }
        getAction(this.url.selectDataDictionary,{parentCode: e,dictionaryTypeId: 'AgreementType'}).then(res => {
          if(res.success){
            this.specificList = res.result.dictionaryList
          }
        })
      },
    }
  }
</script>
